﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace Travel.DAL
{
	/// <summary>
	/// 数据访问类:wb_users
	/// </summary>
	public partial class wb_users
	{
		public wb_users()
		{}
		#region  BasicMethod

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(string id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from wb_users");
			strSql.Append(" where id=@id ");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.NVarChar,50)			};
			parameters[0].Value = id;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(Travel.Model.wb_users model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into wb_users(");
			strSql.Append("id,username,pwd,realname,user_type,qq,email,phone,reg_time,last_log_time,last_log_ip,user_status)");
			strSql.Append(" values (");
			strSql.Append("@id,@username,@pwd,@realname,@user_type,@qq,@email,@phone,@reg_time,@last_log_time,@last_log_ip,@user_status)");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.NVarChar,50),
					new SqlParameter("@username", SqlDbType.NVarChar,50),
					new SqlParameter("@pwd", SqlDbType.NVarChar,50),
					new SqlParameter("@realname", SqlDbType.NVarChar,20),
					new SqlParameter("@user_type", SqlDbType.Int,4),
					new SqlParameter("@qq", SqlDbType.NVarChar,50),
					new SqlParameter("@email", SqlDbType.NVarChar,250),
					new SqlParameter("@phone", SqlDbType.NVarChar,50),
					new SqlParameter("@reg_time", SqlDbType.DateTime),
					new SqlParameter("@last_log_time", SqlDbType.DateTime),
					new SqlParameter("@last_log_ip", SqlDbType.NVarChar,50),
					new SqlParameter("@user_status", SqlDbType.Int,4)};
			parameters[0].Value = model.id;
			parameters[1].Value = model.username;
			parameters[2].Value = model.pwd;
			parameters[3].Value = model.realname;
			parameters[4].Value = model.user_type;
			parameters[5].Value = model.qq;
			parameters[6].Value = model.email;
			parameters[7].Value = model.phone;
			parameters[8].Value = model.reg_time;
			parameters[9].Value = model.last_log_time;
			parameters[10].Value = model.last_log_ip;
			parameters[11].Value = model.user_status;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Travel.Model.wb_users model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update wb_users set ");
			strSql.Append("username=@username,");
			strSql.Append("pwd=@pwd,");
			strSql.Append("realname=@realname,");
			strSql.Append("user_type=@user_type,");
			strSql.Append("qq=@qq,");
			strSql.Append("email=@email,");
			strSql.Append("phone=@phone,");
			strSql.Append("reg_time=@reg_time,");
			strSql.Append("last_log_time=@last_log_time,");
			strSql.Append("last_log_ip=@last_log_ip,");
			strSql.Append("user_status=@user_status");
			strSql.Append(" where id=@id ");
			SqlParameter[] parameters = {
					new SqlParameter("@username", SqlDbType.NVarChar,50),
					new SqlParameter("@pwd", SqlDbType.NVarChar,50),
					new SqlParameter("@realname", SqlDbType.NVarChar,20),
					new SqlParameter("@user_type", SqlDbType.Int,4),
					new SqlParameter("@qq", SqlDbType.NVarChar,50),
					new SqlParameter("@email", SqlDbType.NVarChar,250),
					new SqlParameter("@phone", SqlDbType.NVarChar,50),
					new SqlParameter("@reg_time", SqlDbType.DateTime),
					new SqlParameter("@last_log_time", SqlDbType.DateTime),
					new SqlParameter("@last_log_ip", SqlDbType.NVarChar,50),
					new SqlParameter("@user_status", SqlDbType.Int,4),
					new SqlParameter("@id", SqlDbType.NVarChar,50)};
			parameters[0].Value = model.username;
			parameters[1].Value = model.pwd;
			parameters[2].Value = model.realname;
			parameters[3].Value = model.user_type;
			parameters[4].Value = model.qq;
			parameters[5].Value = model.email;
			parameters[6].Value = model.phone;
			parameters[7].Value = model.reg_time;
			parameters[8].Value = model.last_log_time;
			parameters[9].Value = model.last_log_ip;
			parameters[10].Value = model.user_status;
			parameters[11].Value = model.id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(string id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from wb_users ");
			strSql.Append(" where id=@id ");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.NVarChar,50)			};
			parameters[0].Value = id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from wb_users ");
			strSql.Append(" where id in ("+idlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Travel.Model.wb_users GetModel(string id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 id,username,pwd,realname,user_type,qq,email,phone,reg_time,last_log_time,last_log_ip,user_status from wb_users ");
			strSql.Append(" where id=@id ");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.NVarChar,50)			};
			parameters[0].Value = id;

			Travel.Model.wb_users model=new Travel.Model.wb_users();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Travel.Model.wb_users DataRowToModel(DataRow row)
		{
			Travel.Model.wb_users model=new Travel.Model.wb_users();
			if (row != null)
			{
				if(row["id"]!=null)
				{
					model.id=row["id"].ToString();
				}
				if(row["username"]!=null)
				{
					model.username=row["username"].ToString();
				}
				if(row["pwd"]!=null)
				{
					model.pwd=row["pwd"].ToString();
				}
				if(row["realname"]!=null)
				{
					model.realname=row["realname"].ToString();
				}
				if(row["user_type"]!=null && row["user_type"].ToString()!="")
				{
					model.user_type=int.Parse(row["user_type"].ToString());
				}
				if(row["qq"]!=null)
				{
					model.qq=row["qq"].ToString();
				}
				if(row["email"]!=null)
				{
					model.email=row["email"].ToString();
				}
				if(row["phone"]!=null)
				{
					model.phone=row["phone"].ToString();
				}
				if(row["reg_time"]!=null && row["reg_time"].ToString()!="")
				{
					model.reg_time=DateTime.Parse(row["reg_time"].ToString());
				}
				if(row["last_log_time"]!=null && row["last_log_time"].ToString()!="")
				{
					model.last_log_time=DateTime.Parse(row["last_log_time"].ToString());
				}
				if(row["last_log_ip"]!=null)
				{
					model.last_log_ip=row["last_log_ip"].ToString();
				}
				if(row["user_status"]!=null && row["user_status"].ToString()!="")
				{
					model.user_status=int.Parse(row["user_status"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,username,pwd,realname,user_type,qq,email,phone,reg_time,last_log_time,last_log_ip,user_status ");
			strSql.Append(" FROM wb_users ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" id,username,pwd,realname,user_type,qq,email,phone,reg_time,last_log_time,last_log_ip,user_status ");
			strSql.Append(" FROM wb_users ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM wb_users ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.id desc");
			}
			strSql.Append(")AS Row, T.*  from wb_users T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "wb_users";
			parameters[1].Value = "id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

